//
//  HMEmoticonViewCell.swift
//  Weibo
//
//  Created by teacher on 2017/6/14.
//  Copyright © 2017年 teacher. All rights reserved.
//

import UIKit
//  自定义表情视图cell
class HMEmoticonViewCell: UICollectionViewCell {
    
    //  保存20个表情按钮
    private lazy var emoticonButtonArray: [HMEmoticonButton] = [HMEmoticonButton]()
    
    //  cell对应的indexpath
    var indexPath: IndexPath? {
        didSet {
            guard let currentIndexPath = indexPath else {
                return
            }
            
            messageLabel.text = "当前滚动的是第\(currentIndexPath.section + 1)组第\(currentIndexPath.item + 1)页"
        }
    }
    //  表情模型数组
    var emoticonArray: [HMEmoticon]? {
        didSet {
            guard let currentEmoticonArray = emoticonArray else {
                return
            }
            
            //  隐藏所有的表情按钮
            for button in emoticonButtonArray {
                //  隐藏按钮
                button.isHidden = true
            }
            
            //  遍历模型数组
            for (i, emoticon) in currentEmoticonArray.enumerated() {
                //  根据下标获取表情按钮
                let button = emoticonButtonArray[i]
                //  设置表情模型
                button.emoticon = emoticon
            }
            
        }
    }
    
    //  MARK:   --懒加载控件
    fileprivate lazy var messageLabel: UILabel = {
        let label = UILabel(fontSize: 20, textColor: UIColor.blue)
        return label
    }()
    
    //  删除按钮
    fileprivate lazy var deleteButton: UIButton = {
        let button = UIButton()
        //  添加点击事件
        button.addTarget(self, action: #selector(deleteButtonAction), for: .touchUpInside)
        button.setImage(UIImage(named: "compose_emotion_delete"), for: .normal)
        button.setImage(UIImage(named: "compose_emotion_delete_highlighted"), for: .highlighted)
        return button
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //  添加控件设置约束
    private func setupUI() {
        addChildButton()
//        contentView.addSubview(messageLabel)
//        messageLabel.snp.makeConstraints { (make) in
//            make.center.equalTo(contentView)
//        }
    }
    
    //  添加20个表情按钮
    private func addChildButton() {
        //  设置宽度
        let buttonWith = width / 7
        //  设置高度
        let buttonHeight = height / 3
        
        for i in 0..<20 {
            
            //  计算列的索引
            let colIndex = i % 7
            //  计算行的索引
            let rowIndex = i / 7
            
            let button = HMEmoticonButton()
            //  添加按钮的点击事件
            button.addTarget(self, action: #selector(emoticonButtonAction(btn:)), for: .touchUpInside)
            button.x = CGFloat(colIndex) * buttonWith
            button.y = CGFloat(rowIndex) * buttonHeight
            button.size = CGSize(width: buttonWith, height: buttonHeight)
            
            button.titleLabel?.font = UIFont.systemFont(ofSize: 33)
            
            contentView.addSubview(button)
            //  保存表情按钮
            emoticonButtonArray.append(button)
        }
        
        //  添加删除按钮
        contentView.addSubview(deleteButton)
        //  设置删除按钮的大小和表情大小一样
        deleteButton.size = CGSize(width: buttonWith, height: buttonHeight)
        //  设置x和y坐标
        deleteButton.x = width - buttonWith
        deleteButton.y = height - buttonHeight
    }
    
    //  MARK:   --删除按钮点击事件
    @objc private func deleteButtonAction() {
        //  发送删除表情内容的通知
        NotificationCenter.default.post(name: NSNotification.Name(ClickDeleteButtonNotificationName), object: nil)
    }
    
    //  MARK:   --点击表情按钮的事件
    @objc private func emoticonButtonAction(btn: HMEmoticonButton) {
        //  获取按钮点击的模型
        let emoticon = btn.emoticon!
        //  发送通知
        NotificationCenter.default.post(name: NSNotification.Name(ClickEmotionButtonNotificationName), object: emoticon)
    }
}
